package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

import org.apache.commons.compress.utils.Lists;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.system.domain.WorkArea;
import com.ruoyi.system.domain.WorkEmployPosition;
import com.ruoyi.system.domain.WorkEmployTitle;
import com.ruoyi.system.domain.WorkProfession;
import com.ruoyi.system.domain.vo.WorkEmployExcportDTO;
import com.ruoyi.system.domain.vo.WorkEmployPositionDTO;
import com.ruoyi.system.domain.vo.WorkEmployPositionFutureDTO;
import com.ruoyi.system.domain.vo.WorkEmployPositionNextDTO;
import com.ruoyi.system.domain.vo.WorkEmployTitleDTO;
import com.ruoyi.system.mapper.WorkEmployPositionMapper;
import com.ruoyi.system.mapper.WorkEmployTitleMapper;
import com.ruoyi.system.mapper.WorkProfessionMapper;
import com.ruoyi.system.service.IWorkAreaService;
import com.ruoyi.system.service.IWorkEmployTitleService;
import com.ruoyi.system.service.IWorkProfessionService;

/**
 * 招聘信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-08-14
 */
@Service
public class WorkEmployTitleServiceImpl implements IWorkEmployTitleService 
{
    private final static Logger log = LoggerFactory.getLogger(WorkEmployTitleServiceImpl.class);
    @Autowired
    private WorkEmployTitleMapper workEmployTitleMapper;
    @Autowired
    IWorkAreaService workAreaService;
    
    @Autowired
    WorkEmployPositionMapper workEmployPositionMapper;
    
    @Autowired
    WorkProfessionMapper workProfessionMapper;
    
    
    @Autowired
    IWorkProfessionService workProfessionService;
    List<WorkProfession> workProfessionList =null;
    
    /**
     * 查询招聘信息
     * 
     * @param employId 招聘信息主键
     * @return 招聘信息
     */
    @Override
    public WorkEmployTitle selectWorkEmployTitleByEmployId(Integer employId)
    {
    	WorkEmployTitle workEmployTitle = workEmployTitleMapper.selectWorkEmployTitleByEmployId(employId);
    	List<WorkArea> areaList =	workAreaService.getParentList(workEmployTitle.getArea());
    	List<String> areas = areaList.stream().map(bean -> bean.getAreaCode()).collect(Collectors.toList());

    	workEmployTitle.setAreas(areas);
    	QueryWrapper<WorkEmployPosition> wrapper = new QueryWrapper<>();
    	wrapper.eq("employ_id", employId);
    	List<WorkEmployPosition> list = workEmployPositionMapper.selectList(wrapper);
    	for(WorkEmployPosition a : list) {
    	   	QueryWrapper<WorkProfession> wrapper1 = new QueryWrapper<>();
        	wrapper1.eq("profession_code", a.getProfession());
        	WorkProfession workProfession  = workProfessionMapper.selectOne(wrapper1);
        	if(workProfession != null) {
        		a.setProfessionName(workProfession.getProfessionName());
        	}
        	List<WorkProfession> professionList = 	workProfessionService.getParentList(a.getProfession());
        	List<String> professions = professionList.stream().map(bean -> bean.getProfessionCode()).collect(Collectors.toList());
        	a.setProfessions(professions);
    	}
    	
    	List<WorkEmployPosition> list1 = list.stream().filter(entity -> entity.getPlanType().equals("1")).collect(Collectors.toList());
    	List<WorkEmployPosition> list2 = list.stream().filter(entity -> entity.getPlanType().equals("2")).collect(Collectors.toList());
    	List<WorkEmployPosition> list3= list.stream().filter(entity -> entity.getPlanType().equals("3")).collect(Collectors.toList());
    	workEmployTitle.setPositionList1(list1);
    	workEmployTitle.setPositionList2(list2);
    	workEmployTitle.setPositionList3(list3);
        return workEmployTitle;
    }
    
    private void setWorkEmployPosition(WorkEmployTitle workEmployTitle,Map<String,WorkProfession> workProfessionMap) {
    	QueryWrapper<WorkEmployPosition> wrapper = new QueryWrapper<>();
    	wrapper.eq("employ_id", workEmployTitle.getEmployId());
    	List<WorkEmployPosition> list = workEmployPositionMapper.selectList(wrapper);
    	for(WorkEmployPosition a : list) {

        	WorkProfession workProfession  = workProfessionMap.get(a.getProfession());
        	if(workProfession != null) {
        		a.setProfessionName(workProfession.getProfessionName());
        	}
    	}
    	
    	List<WorkEmployPosition> list1 = list.stream().filter(entity -> entity.getPlanType().equals("1")).collect(Collectors.toList());
    	List<WorkEmployPosition> list2 = list.stream().filter(entity -> entity.getPlanType().equals("2")).collect(Collectors.toList());
    	List<WorkEmployPosition> list3= list.stream().filter(entity -> entity.getPlanType().equals("3")).collect(Collectors.toList());
    	workEmployTitle.setPositionList1(list1);
    	workEmployTitle.setPositionList2(list2);
    	workEmployTitle.setPositionList3(list3);
    }

    /**
     * 查询招聘信息列表
     * 
     * @param workEmployTitle 招聘信息
     * @return 招聘信息
     */
    @Override
    public List<WorkEmployTitle> selectWorkEmployTitleList(WorkEmployTitle workEmployTitle)
    {   
    	if(workProfessionList == null) {
    	   QueryWrapper<WorkProfession> wrapper = new QueryWrapper<>();
           workProfessionList = workProfessionMapper.selectList(wrapper);
    	}
    	Map<String,WorkProfession> workProfessionMap= workProfessionList.stream().collect(Collectors.toMap(WorkProfession::getProfessionCode, account -> account, (k1, k2) -> k1));
 	   PageUtils.startPage();
    	List<WorkEmployTitle> list = workEmployTitleMapper.selectWorkEmployTitleList(workEmployTitle);
    	for(WorkEmployTitle title : list) {
    		this.setWorkEmployPosition(title,workProfessionMap);
    	}
        return list;
    }

    /**
     * 新增招聘信息
     * 
     * @param workEmployTitle 招聘信息
     * @return 结果
     */
    @Override
    public WorkEmployTitle insertWorkEmployTitle(WorkEmployTitle workEmployTitle)
    {
    	String area = workEmployTitle.getArea();
    	if(workEmployTitle.getAreas() != null && !workEmployTitle.getAreas().isEmpty()) {
    		area = workEmployTitle.getAreas().get(workEmployTitle.getAreas().size()-1);
    	}
    	workEmployTitle.setArea(area);
        workEmployTitle.setCreateTime(DateUtils.getNowDate());
        workEmployTitleMapper.insertWorkEmployTitle(workEmployTitle);
        addEmployPosition(workEmployTitle, workEmployTitle.getPositionList1());
        addEmployPosition(workEmployTitle, workEmployTitle.getPositionList2());
        addEmployPosition(workEmployTitle, workEmployTitle.getPositionList3());
        return workEmployTitle;
    }

    /**
     * 修改招聘信息
     * 
     * @param workEmployTitle 招聘信息
     * @return 结果
     */
    @Override
    public int updateWorkEmployTitle(WorkEmployTitle workEmployTitle)
    {
        workEmployTitle.setUpdateTime(DateUtils.getNowDate());
    	String area = "";
    	if(!workEmployTitle.getAreas().isEmpty()) {
    		area = workEmployTitle.getAreas().get(workEmployTitle.getAreas().size()-1);
    	}
    	workEmployTitle.setArea(area);
    	QueryWrapper<WorkEmployPosition> wrapper = new QueryWrapper<>();
    	wrapper.eq("employ_id", workEmployTitle.getEmployId());
        workEmployPositionMapper.delete(wrapper);
        addEmployPosition(workEmployTitle, workEmployTitle.getPositionList1());
        addEmployPosition(workEmployTitle, workEmployTitle.getPositionList2());
        addEmployPosition(workEmployTitle, workEmployTitle.getPositionList3());
        
        return workEmployTitleMapper.updateWorkEmployTitle(workEmployTitle);
    }
    
    private void addEmployPosition(WorkEmployTitle workEmployTitle, List<WorkEmployPosition> list) {
    	 for(WorkEmployPosition position : list) {
    		 position.setPositionId(null);
    	   position.setEmployId(workEmployTitle.getEmployId());
    	   String proession = position.getProfession();
    	   if(position.getProfessions()!=null && !position.getProfessions().isEmpty()) {
    		 proession = position.getProfessions().get(position.getProfessions().size()-1);
    	   }
    	   position.setProfession(proession);
    	   workEmployPositionMapper.insert(position);
    	 }
    }

    /**
     * 批量删除招聘信息
     * 
     * @param employIds 需要删除的招聘信息主键
     * @return 结果
     */
    @Override
    public int deleteWorkEmployTitleByEmployIds(Integer[] employIds)
    {  
    	for(int id : employIds) {
    		deleteWorkEmployTitleByEmployId(id);
    	}
    	return 1;
    }

    /**
     * 删除招聘信息信息
     * 
     * @param employId 招聘信息主键
     * @return 结果
     */
    @Override
    public int deleteWorkEmployTitleByEmployId(Integer employId)
    {
    	QueryWrapper<WorkEmployPosition> wrapper = new QueryWrapper<>();
    	wrapper.eq("employ_id", employId);
    	workEmployPositionMapper.delete(wrapper);
    	
        return workEmployTitleMapper.deleteWorkEmployTitleByEmployId(employId);
    }

	@Override
	public void importWorkEmployTitle(List<WorkEmployTitleDTO> workEmployTitleDTOList,List<WorkEmployPositionDTO> workEmployPositionDTO1,
			List<WorkEmployPositionNextDTO> workEmployPositionDTO2,
			List<WorkEmployPositionFutureDTO> workEmployPositionDTO3) {
	
		
	    Map<String,WorkEmployTitleDTO> workEmployTitleMap= workEmployTitleDTOList.stream().collect(Collectors.toMap(WorkEmployTitleDTO::getCompanyName, account -> account, (k1, k2) -> k1));
	    
		if(workEmployTitleMap.size()>1) {
		    throw new ServiceException("同时只允许导入一个单位！");
		}
	    List<WorkProfession> workProfessionList = workProfessionService.list();
	    Map<String,WorkProfession> workProfessionMap= workProfessionList.stream()
	    		.filter(user -> !user.getParentCode().equals("0")).collect(Collectors.toMap(WorkProfession::getProfessionName, account -> account, (k1, k2) -> k1));
	    QueryWrapper<WorkArea> wrapper = new QueryWrapper<>();
    	wrapper.like("area_code", "350");
		List<WorkArea> workAreaList = workAreaService.list(wrapper);
	    Map<String,WorkArea> workAreaMap= workAreaList.stream().collect(Collectors.toMap(WorkArea::getName, account -> account, (k1, k2) -> k1));
	    int year = getCurrentYear();
        String lable1 = year+"年计划招聘";
        String lable2 = (year+1)+"年计划招聘";
        String lable3 = (year+2)+"至"+(year+5)+"年计划招聘";
        StringBuffer sb = new StringBuffer();
		// 填充map
        List<WorkEmployTitle> workEmployTitleList = Lists.newArrayList(); 
        
		for (Map.Entry<String, WorkEmployTitleDTO> entry : workEmployTitleMap.entrySet()) {
			log.info("Key = " + entry.getKey() + ", Value = " + entry.getValue());
			WorkEmployTitleDTO title = entry.getValue();
			if(!check(sb, title, workAreaMap, workProfessionMap)) {
				log.warn("importtitle check title:{}, sb:{}." ,title, sb);
				continue;
			}
			WorkEmployTitle workEmployTitle = new WorkEmployTitle();
		    List<WorkEmployPosition> positionList1 = Lists.newArrayList();
		    List<WorkEmployPosition> positionList2 = Lists.newArrayList();
		    List<WorkEmployPosition> positionList3 = Lists.newArrayList();
		    int i=3;
		    workEmployTitle.setCompanyName(title.getCompanyName());
			workEmployTitle.setCompanyType(title.getCompanyType());
			workEmployTitle.setYear(String.valueOf(year));

			String[] areas = title.getAreaName().split("\\\\");
			WorkArea workArea = workAreaMap.get(areas[1]);
			
			workEmployTitle.setArea(workArea.getAreaCode());					
			workEmployTitle.setCompanyName(title.getCompanyName());
			workEmployTitle.setCreateTime(new Date());
			workEmployTitle.setCurrentPostCount(title.getCurrentPostCount());
			workEmployTitle.setCurrentInnerCount(title.getCurrentInnerCount());
			workEmployTitle.setNextPostCount(title.getNextPostCount());
			workEmployTitle.setNextInnerCount(title.getNextInnerCount());
			workEmployTitle.setFuturePostCount(title.getFuturePostCount());
			workEmployTitle.setFutureInnerCount(title.getFutureInnerCount());
		
			for (WorkEmployPositionDTO position : workEmployPositionDTO1) {
				if(!check(sb, position, workAreaMap, workProfessionMap)) {
					log.warn("imporposition check position:{}, sb:{}." ,title, sb);
					continue;
				}
				WorkEmployPosition workEmployPosition1 = new WorkEmployPosition();
				workEmployPosition1.setPlanType("1");
				WorkProfession workProfession1 = workProfessionMap.get(position.getProfessionName());

				workEmployPosition1.setPositionYear(lable1);
				workEmployPosition1.setEducationaRequirement(position.getEducationaRequirement());
				workEmployPosition1.setProfession(workProfession1.getProfessionCode());
				workEmployPosition1.setWorkCount(position.getWorkCount());
				workEmployPosition1.setHasCount(position.getHasCount());
				workEmployPosition1.setRemark(position.getRemark());
				positionList1.add(workEmployPosition1);
			}
			for (WorkEmployPositionNextDTO dto :workEmployPositionDTO2) {
				WorkEmployPositionDTO position= toEntity(dto, WorkEmployPositionDTO.class);
				if(!check(sb, position, workAreaMap, workProfessionMap)) {
					log.warn("imporposition check position:{}, sb:{}." ,title, sb);
					continue;
				}
				WorkEmployPosition workEmployPosition = new WorkEmployPosition();
				workEmployPosition.setPlanType("2");
				WorkProfession workProfession1 = workProfessionMap.get(position.getProfessionName());

				workEmployPosition.setPositionYear(lable1);
				workEmployPosition.setEducationaRequirement(position.getEducationaRequirement());
				workEmployPosition.setProfession(workProfession1.getProfessionCode());
				workEmployPosition.setWorkCount(position.getWorkCount());
				workEmployPosition.setRemark(position.getRemark());
				positionList2.add(workEmployPosition);
			}
			for (WorkEmployPositionFutureDTO dto : workEmployPositionDTO3) {
				WorkEmployPositionDTO position= toEntity(dto, WorkEmployPositionDTO.class);
				if(!check(sb, position, workAreaMap, workProfessionMap)) {
					log.warn("imporposition check position:{}, sb:{}." ,title, sb);
					continue;
				}
				WorkEmployPosition workEmployPosition = new WorkEmployPosition();
				workEmployPosition.setPlanType("3");
				WorkProfession workProfession1 = workProfessionMap.get(position.getProfessionName());

				workEmployPosition.setPositionYear(lable1);
				workEmployPosition.setEducationaRequirement(position.getEducationaRequirement());
				workEmployPosition.setProfession(workProfession1.getProfessionCode());
				workEmployPosition.setWorkCount(position.getWorkCount());
				workEmployPosition.setRemark(position.getRemark());
				positionList3.add(workEmployPosition);
	
			}
			
			workEmployTitle.setPositionList1(positionList1);
			workEmployTitle.setPositionList2(positionList2);
			workEmployTitle.setPositionList3(positionList3);
			workEmployTitleList.add(workEmployTitle);
			
			check(sb, title,positionList1,positionList2,positionList3);
		}
		
		if(!sb.toString().isEmpty()) {
			 throw new ServiceException(sb.toString());	
		}
		
		for(WorkEmployTitle work : workEmployTitleList) {
			   insertWorkEmployTitle(work);
		}
		
	}
	
	private boolean check(StringBuffer sb, WorkEmployTitleDTO title, Map<String,WorkArea> workAreaMap, Map<String,WorkProfession> workProfessionMap) {
		String s="";
	    int year = getCurrentYear();
        String lable1 = year+"年计划招聘";
        String lable2 = (year+1)+"年计划招聘";
        String lable3 = (year+2)+"至"+(year+5)+"年计划招聘";
		if(title.getCompanyName() == null || title.getCompanyName().isEmpty()) {
			s = s+"单位名称填写不正确,";
		}
		if(title.getCompanyType() == null || title.getCompanyType().isEmpty()) {
			s = s+"单位类型填写不正确,";
		}
		String[] areas = title.getAreaName().split("\\\\");
		if(areas.length<2) {
			s = s+"地区["+title.getAreaName()+"]填写不正确,";
		}
		
		WorkArea workArea = workAreaMap.get(areas[1]);
		if(workArea == null) {
			s = s+"地区["+title.getAreaName()+"]填写不正确,";
		}
		
		if(title.getCurrentPostCount() == null || title.getCurrentPostCount()==0) {
			s = s+lable1+"数量填写不正确,";
		}
		if(title.getNextPostCount() == null || title.getNextPostCount()==0) {
			s = s+lable2+"数量填写不正确,";
		}
		if(title.getFuturePostCount() == null || title.getFuturePostCount()==0) {
			s = s+lable3+"数量填写不正确,";
		}
		sb.append(s);
		return s.isEmpty();
	}
	
	private boolean check(StringBuffer sb, WorkEmployPositionDTO title, Map<String,WorkArea> workAreaMap, Map<String,WorkProfession> workProfessionMap) {
		String s="";
	    int year = getCurrentYear();
        String lable1 = year+"年计划招聘";
        String lable2 = (year+1)+"年计划招聘";
        String lable3 = (year+2)+"至"+(year+5)+"年计划招聘";
	
		if(StringUtils.isEmpty(title.getEducationaRequirement())) {
			s = s+lable1+"专业要求填写不正确,";
		}

		
		WorkProfession workProfession1 = workProfessionMap.get(title.getProfessionName());
		if(workProfession1 == null) {
			s = s+"专业["+title.getProfessionName()+"]填写不正确,";
		}

		if(title.getWorkCount() == null || title.getWorkCount()==0) {
			s = s+lable1+title.getProfessionName()+"要求数量不正确,";
		}
		sb.append(s);
		return s.isEmpty();
	}
	private boolean check(StringBuffer sb, WorkEmployTitleDTO title, List<WorkEmployPosition> positionList1,
			List<WorkEmployPosition> positionList2
			, List<WorkEmployPosition> positionList3) {
		String s="";
	    int year = getCurrentYear();
        String lable1 = year+"年计划招聘";
        String lable2 = (year+1)+"年计划招聘";
        String lable3 = (year+2)+"至"+(year+5)+"年计划招聘";
        int count1=0;
        for(WorkEmployPosition p : positionList1) {
        	count1 = count1 + p.getWorkCount().intValue();
        }
        
        int count2=0;
        for(WorkEmployPosition p : positionList2) {
        	count2 = count2 + p.getWorkCount().intValue();
        }
        
        int count3=0;
        for(WorkEmployPosition p : positionList3) {
        	count3 = count3 + p.getWorkCount().intValue();
        }
        
        
		if(title.getCurrentPostCount()!=count1) {
			s = s+lable1+"数量填写不正确,";
		}
		if(title.getNextPostCount()!=count2) {
			s = s+lable2+"数量填写不正确,";
		}
		if(title.getFuturePostCount()!=count3) {
			s = s+lable3+"数量填写不正确,";
		}
	
		sb.append(s);
		return s.isEmpty();
	}
	private int getCurrentYear() {
		Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        return year;
	}

	@Override
	public WorkEmployExcportDTO selectWorkEmployTitleListExport(WorkEmployTitle workEmployTitle) {
		
		List<WorkEmployTitle> list = workEmployTitleMapper.selectWorkEmployTitleList(workEmployTitle);
		WorkEmployExcportDTO export = new WorkEmployExcportDTO();
        String year = workEmployTitle.getYear();
        String lable1 = year+"年计划招聘";
        String lable2 = (Integer.parseInt(year)+1)+"年计划招聘";
        String lable3 = (Integer.parseInt(year)+2)+"至"+(Integer.parseInt(year)+5)+"年计划招聘";
        
        
        List<String> dataNames = Lists.newArrayList();
        dataNames.add("基本信息");
        dataNames.add(lable1);
        dataNames.add(lable2);
        dataNames.add(lable3);
        
    	List models = Lists.newArrayList();
    	models.add(WorkEmployTitleDTO.class);
    	models.add(WorkEmployPositionDTO.class);
    	models.add(WorkEmployPositionNextDTO.class);
    	models.add(WorkEmployPositionNextDTO.class);
    	
    	List datas  = Lists.newArrayList(); 
    	
		List<WorkEmployPositionDTO> workEmployPositionList1 = Lists.newArrayList();
		List<WorkEmployPositionNextDTO> workEmployPositionList2 = Lists.newArrayList();
		List<WorkEmployPositionNextDTO> workEmployPositionList3= Lists.newArrayList();
		
		for(WorkEmployTitle title : list) {
			WorkEmployPosition wrapper = new WorkEmployPosition();
			wrapper.setEmployId(title.getEmployId());
	  		if(title.getCurrentInnerCount() == null) {
	  			title.setCurrentInnerCount(0L);
    		}
	  		if(title.getNextInnerCount() == null) {
	  			title.setNextInnerCount(0L);
    		}
	  		if(title.getFutureInnerCount() == null) {
	  			title.setFutureInnerCount(0L);
    		}
	    	List<WorkEmployPosition> positionList = workEmployPositionMapper.selectWorkEmployPositionList(wrapper);
	    	for(WorkEmployPosition a : positionList ) {
	    		if(a.getWorkCount() == null) {
	    			a.setWorkCount(0L);
	    		}
	    		if(a.getHasCount() == null) {
	    			a.setHasCount(0L);
	    		}
	    		a.setCompanyName(title.getCompanyName());
	    	}
	    	Map<String,List<WorkEmployPosition>> workEmployTitleMap= positionList.stream().collect(Collectors.groupingBy(WorkEmployPosition::getPlanType));;

	    	String s = JSON.toJSONString(title);
	    	List<WorkEmployPosition> workEmployPosition1 = workEmployTitleMap.get("1");
	    	if(workEmployPosition1 == null) {
	    		workEmployPosition1 = Lists.newArrayList();
	    	}
	    	List<WorkEmployPosition> workEmployPosition2 = workEmployTitleMap.get("2");
	    	if(workEmployPosition2== null) {
	    		workEmployPosition2 = Lists.newArrayList();
	    	}
	    	List<WorkEmployPosition> workEmployPosition3 = workEmployTitleMap.get("3");
	    	if(workEmployPosition3 == null) {
	    		workEmployPosition3 = Lists.newArrayList();
	    	}
	    	List<WorkEmployPositionDTO> dtoList1 = toEntity(workEmployPosition1, WorkEmployPositionDTO.class);
	    	List<WorkEmployPositionNextDTO> dtoList2 = toEntity(workEmployPosition2, WorkEmployPositionNextDTO.class);
	    	List<WorkEmployPositionNextDTO> dtoList3 = toEntity(workEmployPosition3, WorkEmployPositionNextDTO.class);
	    	
	    	workEmployPositionList1.addAll(dtoList1);
	    	workEmployPositionList2.addAll(dtoList2);
	    	workEmployPositionList3.addAll(dtoList3);
	    	
	    	
		}
		

		List<WorkEmployTitleDTO> dtoList = toEntity(list, WorkEmployTitleDTO.class);

    	datas.add(dtoList);
    	datas.add(workEmployPositionList1);
    	datas.add(workEmployPositionList2);
    	datas.add(workEmployPositionList3);
    	
    	export.setDataNames(dataNames);
    	export.setDatas(datas);
    	export.setModules(models);
		return export;
	}
	
	

    public static <T> T toEntity(Object value, Class<T> clazz) {
        try {
            Object object = null;
            if (value instanceof Optional) {
                if(((Optional)value).isPresent()) {
                    object = ((Optional)value).get();
                }
            } else {
                object = value;
            }
            T t = copyProperties(object, clazz);
            return t;
        } catch (Exception ex) {
        	log.error("toEntity1 ex", ex);
        }
        return null;
    }

	private static <T> T copyProperties(Object value, Class<T> clazz) {
		try {
			T t = clazz.newInstance();
			   ObjectMapper mapper = new ObjectMapper();

	             // 序列化为 JSON 字符串
			   String content = mapper.writeValueAsString(value);
			t = JSON.parseObject(content, clazz);
			return t;
		} catch (Exception ex) {
			log.error("copyProperties ex", ex);
		}
		return null;
	}
	
    public static <T> List<T> toEntity(List<?> value, Class<T> clazz) {
        List list = new ArrayList<>();
        try {

            for (Object o : value) {                
                T t = copyProperties(o, clazz);
                list.add(t);
            }
        } catch (Exception ex) {
        	log.error("toEntity3 ex", ex);
        }
        return list;
    }
    
}
